package com.zmj.sy.mom.srv.aps.controller;

import cn.hutool.core.collection.CollUtil;
import com.zmj.sy.mom.srv.aps.bean.entity.order.Order;
import com.zmj.sy.mom.srv.aps.bean.vo.aps.SelectConsVo;
import com.zmj.sy.mom.srv.aps.bean.vo.apsorderwork.ApsWorkOrderListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.apsorderwork.ApsWorkOrderTreeResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.purchaseplan.ApsWorkOrderPurchaseExcelVo;
import com.zmj.sy.mom.srv.aps.bean.vo.purchaseplan.ApsWorkOrderPurchasePlanReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.purchaseplan.ApsWorkOrderPurchasePlanResVo;
import com.zmj.sy.mom.srv.aps.mapper.OrderMapper;
import com.zmj.sy.mom.srv.aps.service.ApsWorkOrderService;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Api(tags = "APS工单")
@RestController
@RequestMapping("/apsWorkOrder")
public class ApsWorkOrderController {

    private final ApsWorkOrderService apsWorkOrderService;
    private final OrderMapper orderMapper;

    @ApiOperation("排产工单树")
    @GetMapping("/tree")
    public BaseListResVo<ApsWorkOrderTreeResVo> tree(BaseIdReqVo reqVo){
        return apsWorkOrderService.tree(reqVo);
    }

    @ApiOperation("排产右侧列表")
    @GetMapping("/list")
    public BaseListResVo<ApsWorkOrderListResVo> list(BaseIdReqVo reqVo){
        return apsWorkOrderService.list(reqVo);
    }

    @ApiOperation("外购件物料需求计划")
    @GetMapping("/purchasePlan")
    public BasePageResVo<ApsWorkOrderPurchasePlanResVo> purchasePlan(ApsWorkOrderPurchasePlanReqVo reqVo){
        return apsWorkOrderService.purchasePlan(reqVo);
    }

    @ApiOperation("导出 excel")
    @GetMapping("/exportExcel")
    public ResponseEntity<byte[]> exportExcel(ApsWorkOrderPurchaseExcelVo reqVo) throws IOException {

        List<Order> orderList = orderMapper.lambdaQuery().eq(Order::getConstructNo, reqVo.getConstructNo()).list();
        if (CollUtil.isEmpty(orderList)) {
            throw SyExceptionUtils.e("请输入正确施工号。");
        }
        List<String> collect = orderList.stream().map(Order::getConstructNo).distinct().collect(Collectors.toList());
        if (collect.size()>1) {
            throw SyExceptionUtils.e("该号码下多个施工号，请确认后输入。");
        }

        try(Workbook workbook = apsWorkOrderService.exportExcel(reqVo)) {
            String fileName = collect.get(0) + "-外购件领料单" + ".xlsx";
            String fileNameEncoded = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            workbook.write(bytes);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDisposition(ContentDisposition.builder("attachment").filename(fileNameEncoded).build());
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(bytes.toByteArray(), headers, HttpStatus.OK);
        }
    }

    @ApiOperation("施工号下拉框")
    @GetMapping("/selectCon")
    public BaseListResVo<SelectConsVo> selectCon(SelectConsVo reqVo) {
        return apsWorkOrderService.selectCon(reqVo);
    }

}
